package com.liusy.serachengine;



import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import com.liusy.taskengine.executemodel.AbstractExecuteModel;



/**
 * 
 * 抽像的索引创建者
 * 
 * @author liusy 2013-02-19
 *
 */
public abstract class AbstractIndexModel implements IIndexModel{
  
	  private static Logger log = Logger.getLogger(AbstractExecuteModel.class);
	
	  //索引目录
	  protected String indexPath = System.getProperty("user.dir")+"/index/";
	  
	  //分析词
	  private Analyzer analyzer = null;
	  
	  //写入索引配置
	  private IndexWriterConfig iwc = null;
	  
	  //索引目录
	  private Directory dir = null;
	  
	  //写入索引
	  private  IndexWriter writer = null;
	  
	  //搜索器
	  private IndexSearcher indexSearcher= null;
	  
	  
	  private boolean closeWriter  = false;
	  
	  private IConvertModel cm = null;
	 
	public AbstractIndexModel(String indexPath) {
		this.indexPath += indexPath;
		try {
			File fileIndexDir = new File(this.indexPath);
			if(!fileIndexDir.exists())
				fileIndexDir.mkdirs();
			dir = FSDirectory.open(new File(this.indexPath));
			analyzer = new StandardAnalyzer(Version.LUCENE_43);
			iwc = new IndexWriterConfig(Version.LUCENE_43, analyzer);
			
		} catch (Exception e) {
			log.debug(e.getMessage());
			e.printStackTrace();
		}

	}
	  
	
	  public void CreateIndex(IConvertModel cm,boolean create) {
	    try {
	    	if(null == cm || null == cm.getCreateIndexTask() || cm.isFinish())
	    		return ;
	    	this.cm = cm;
	      if (create) {
	        iwc.setOpenMode(OpenMode.CREATE);
	      } else {
	        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
	      }
	      if(null == writer)
		    	writer = new IndexWriter(dir, iwc);
	      
	      WriterDoc(writer,cm.getCreateIndexTask());
	      
	      cm.complete();
	      
	    } catch (IOException e) {
	    	log.info(e.getMessage());
	    	log.error(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
	    }
	  }
  
  	/**
  	 * 是否已经关闭写入流
  	 * 
  	 * @return
  	 */
  	public boolean isCloseWriter()
  	{
  		return this.closeWriter;
  	}
  	
  	
  	/**
  	 * 返回使用的分析词
  	 * 
  	 * @return
  	 */
  	public Analyzer getAnalyzer()
  	{
  		return this.analyzer;
  	}
  	
  	
  	/**
  	 * 关闭写入 流
  	 */
  	public void setCloseWriter()
  	{
  		try {
  			closeWriter = false;
  			if(null!=this.writer)
			this.writer.close();
			writer = null;
		} catch (IOException e) {
			log.info(e.getMessage());
			e.printStackTrace();
		}finally{
			writer = null;
		}
  	}
  	
  	
  	/**
  	 * 反回当前建索引目录
  	 * 
  	 */
  	public String getIndexPath() {
		return indexPath;
	}

	/**
  	 * 得到搜索器
  	 * 
  	 * 
  	 * @return
  	 */
  	public IndexSearcher getIndexSearcher()
  	{
  		if(null ==indexSearcher)
  			UpdateIndexSearcher();
  	    return  indexSearcher;
  	}
  	
  	/**
  	 * 更新搜索器
  	 * 
  	 * 
  	 * @return
  	 */
  	public void UpdateIndexSearcher()
  	{
  	    IndexReader reader = null;
		try {
			reader = DirectoryReader.open(dir);
			indexSearcher= null;
		} catch (IOException e) {
			log.info(e.getMessage());
			e.printStackTrace();
		}
		indexSearcher = new IndexSearcher(reader);
  	}
  	
	/**
	 * 根据目录,创建文件建索引
	 * 
	 * @param writer
	 * @param file
	 * @throws IOException
	 */
	protected abstract void WriterDoc(IndexWriter writer, Object obj);
	
	
	/**
	 * 文件类型暂时不处理（doc,zip等）
	 * 
	 * @param file
	 * @return
	 */
	@Override
	public IConvertModel getConvertModel(){
		return this.cm;
	}
}
